/**
 * 基础类,处理公共方法,以及纹理等
 */
BaseLayer = cc.Layer.extend({
	nodeName:"未命名",
	ctor:function (parent) {
		this._super();
		parent.addChild(this, 10);
	},
	init:/**
			 * 初始化,
			 */
		function () {
		cc.log("BaseLayer初始化");
		ll.run = this;
		this.callNext = cc.callFunc(function(){
			gg.flow.next();
		}, this);
		this.callKill = cc.callFunc(function(p){
			var seq = cc.sequence(cc.fadeOut(0.5),cc.callFunc(function(){
				p.removeFromParent(true);	
			}, this));
			p.runAction(seq);
		}, this);
		this.callNext.retain();
		this.callKill.retain();

		this.clock = new Clock(this);
	},
	showTitle:function(){
		this.nodeName = gg.flow.flow.nodeName;
		var title = new cc.Node();
		this.addChild(title, 10);
		var content = new cc.LabelTTF(this.nodeName, gg.fontName, gg.fontSize);
		content.setColor(cc.color(255, 255, 255));
		title.addChild(content,2);
		var mr = 10;
		var rect = content.getBoundingBoxToWorld();
		var bg = new cc.Scale9Sprite(res_start.show_tip);
		bg.width = rect.width + mr * 2;
		bg.height = rect.height + mr * 2;
		bg.setPosition(rect.x + rect.width * 0.5, rect.y + rect.height * 0.5);
		title.addChild(bg);
		title.setPosition(gg.c_width, gg.height * 0.95);
		title.setCascadeOpacityEnabled(true);
		title.setOpacity(0);
		title.runAction(cc.fadeIn(0.5));
	},
	initIron:function(){
		this.ironNode = new cc.Node();
		this.ironNode.setCascadeOpacityEnabled(true);
		this.addChild(this.ironNode,8);

		var iron = new Button(this.ironNode, 10, TAG_IRON, "#ironShelf.png");
		iron.setPosition(cc.p(200, 300));

		var funnel = new Button(this.ironNode, 11, TAG_FUNNEL, "#funnel.png");
		funnel.setPosition(cc.p(200, 250));

		var paper = new cc.Sprite("#filter_paper2.png");
		this.ironNode.addChild(paper, 20);
		paper.setPosition(cc.p(200, 285));

		var rod = new Button(this.ironNode, 12, TAG_ROD2, "#rod.png",this.callback);
		rod.setRotation(20);
		rod.setPosition(cc.p(185,355));
	},
	initLamp:function(){
		this.lampNode = new cc.Node();
		this.lampNode.setCascadeOpacityEnabled(true);
		this.addChild(this.lampNode,8,TAG_LAMP_NODE);

		var shelf = new cc.Sprite("#shelf.png");
		this.lampNode.addChild(shelf,3);
		shelf.setPosition(650,200);
		
		var net = new cc.Sprite("#asbestosNet.png");
		this.lampNode.addChild(net,4);
		net.setPosition(645,255);

		var lamp = new Lamp(this);
		lamp.setPosition(800,190);
		lamp.openLidResult();
		lamp.putLampResult();
		lamp.loadFire();
	},
	loadInLib:/**
				 * 从药品库里取出,并下一步
				 * 
				 * @param obj
				 *            对象
				 * @param pos
				 *            原位置
				 * @param tarPos
				 *            目标位置
				 * @param delay
				 *            耗时
				 */
	function(obj, pos, tarPos,delay){
		obj.setPosition(pos);
		if(delay == null){
			delay = 1;
		}
		var ber = $.bezier(pos, tarPos, delay);
		var seq = cc.sequence(ber, this.callNext);
		obj.runAction(seq);
	},
	loadInLib2:/**
				 * 从药品库里取出,自定义返回
				 * 
				 * @param obj
				 * @param pos
				 * @param tarPos
				 * @param delay
				 */
	function(obj, pos, tarPos,delay, diyCall){
		obj.setPosition(pos);
		if(delay == null){
			delay = 1;
		}
		var ber = $.bezier(pos, tarPos, delay);
		var seq = cc.sequence(ber, diyCall);
		obj.runAction(seq);
	},
	destroy:/**
			 * 销毁
			 */
		function(){
		this.callNext.release();	
		this.callKill.release();
		this.showFinish();
		if(this.lib != null){
			this.lib.close();
		}
		this.scheduleOnce(function(){
			this.removeFromParent(true);	
		}, 2);
	},
	showFinish:/**
				 * 展示结束
				 */
		function(){
		if(gg.teach_type == TAG_LEAD){
			var finish = new ShowTip(this);
			finish.init(this.nodeName + " 已完成", 1.5)
			finish.setPosition(gg.c_p);
		}
	},
	beakerToNet:/**
				 * 把烧杯挪到石棉网
				 */
		function(p){
		var move = cc.moveTo(0.6, cc.p(645, 290));
		var seq = cc.sequence(move, this.callNext);
		p.runAction(seq);
	},
	beakerYinliu:/**
					 * 烧杯引流
					 */
		function(p, tag){
		// 通过引流棒过滤
		var bezier = cc.bezierTo(1, [cc.p(520,340),cc.p(385,540),cc.p(228,375)]);
		var rotate = cc.rotateTo(0.8, -90);
		var seq = cc.sequence(bezier,cc.callFunc(function(){
			// 烧杯水位线下降
			p.pour();
			p.hide(3);
			// 烧杯水位线上升
			var beaker = this.getChildByTag(tag);
			beaker.upLine(true);
		}, this),rotate);
		p.runAction(seq);
	},
	loadPaper3:function(pos){
		var paper3 = new Button(this, 10, TAG_PAPER3,"#paper.png", this.callback);
		paper3.setPosition(cc.p(960,600));
		var seq=cc.sequence(cc.moveTo(0.6,cc.p(1000,310)),cc.callFunc(function() {
			paper3.removeFromParent();
			var paper = this.getChildByTag(TAG_PAPER);
			paper.setVisible(true);
			var balance = this.getChildByTag(TAG_BALANCE);
			balance.inPaper();
			balance.showDisk(2);
		}, this));
		paper3.runAction(seq);
	},
	loadBox:function(pos){
		var box = new Button(this, 10, TAG_BOX,"#famahe.png", this.callback);
		box.setPosition(cc.p(960,600));
		box.setScale(0.5);
		var seq=cc.sequence(cc.moveTo(0.6, cc.p(600,300)),cc.callFunc(function() {
			box.setSpriteFrame("famahe_open.png");
			box.setScale(1);
		}, this),cc.delayTime(0.5),cc.callFunc(function() {
			var fa = new cc.Sprite("#fama.png");
			fa.setPosition(cc.p(600,290));
			this.addChild(fa,11);
			var se=cc.sequence(cc.delayTime(0.5),cc.moveTo(0.6, cc.p(1077,290)),cc.callFunc(function() {
				fa.removeFromParent();
				var balance = this.getChildByTag(TAG_BALANCE);
				balance.inFama();
			}, this));
			fa.runAction(se);
		}, this),cc.delayTime(1.5),cc.callFunc(function() {
			box.removeFromParent();
		}, this),this.callNext);
		box.runAction(seq);
	},
	loadSalt:function(pos){
		this.salt = new SampleBottle(this);
		this.addChild(this.salt, 20);
		this.loadInLib(this.salt, pos, cc.p(600, 250));
	},
	loadCyl:function(pos){
		var cylinder = new Button(this, 10, TAG_CYLINDER,"#cylinder.png", this.callback);
		this.loadInLib(cylinder, pos, cc.p(280,150));
	},
	loadKettle:function(pos){
		var kettle = new Button(this, 10, TAG_KETTLE,"#kettle.png", this.callback);
		var tag = new LabelTag(kettle, "蒸馏水");
		tag.setPosition(kettle.width * 0.5, kettle.height * 0.4);
		this.loadInLib(kettle, pos, cc.p(700,200));
	},
	loadIron:function(pos){
		this.ironNode = new cc.Node();
		this.ironNode.setCascadeOpacityEnabled(true);
		this.addChild(this.ironNode,8);

		var iron = new Button(this.ironNode, 10, TAG_IRON, "#ironShelf.png");
		this.loadInLib(iron, pos, cc.p(200, 300));
	},
	loadBeaker:function(pos){
		var action = null;
		if(gg.flow.flow != null){
			action = gg.flow.flow.action;
		}
		if(action == ACTION_DO1){
			var beaker = new Beaker(this, 10, TAG_BEAKER, this.callback); 
			this.loadInLib(beaker, pos, cc.p(550, 140));
		} else if(action == ACTION_DO2){
			var beaker = new Beaker(this, 10, TAG_BEAKER2, this.callback); 
			this.loadInLib(beaker, pos, cc.p(180, 185));
		} else if(action == ACTION_DO3){
			var beaker = new Beaker(this, 10, TAG_BEAKER3, this.callback); 
			this.loadInLib(beaker, pos, cc.p(180, 185));
		} else if(action == ACTION_NONE){
			var beaker = new Beaker(this, 10, TAG_BEAKER4, this.callback); 
			this.loadInLib(beaker, pos, cc.p(180, 185));
		}
	},
	loadFunnel:function(pos){
		var funnel = new Button(this.ironNode, 11, TAG_FUNNEL, "#funnel.png");
		this.loadInLib(funnel, pos, cc.p(200, 250));
	},
	showPaper:function(pos){
		var filter = new Win(this,"#filter_paper.png");
		filter.show(true);
	},
	showPaper2:function(pos){
		var filter = new Win(this,"#paper_funnel.png");
		filter.show(false);
	},
	loadPaper:function(pos){
		if(pos == null){
			pos = this.lib.getPosition();
		}
		var paper = new cc.Sprite("#filter_paper2.png");
		this.ironNode.addChild(paper, 20);
		this.loadInLib(paper, pos, cc.p(200, 285));
	},
	loadPaper2:function(pos){
		var paper = new Button(this, 7, TAG_PAPER2, "#paper.png",this.callback);
		this.loadInLib(paper, pos, cc.p(700, 220));
	},
	loadRod:function(pos){
		var rod = new Button(this, 12, TAG_ROD, "#rod.png",this.callback);
		var call = cc.callFunc(function(){
			this.doRod(rod);
		}, this);
		this.loadInLib2(rod, pos, cc.p(700, 500), 0.5, call);
	},
	doRod:function(){
		gg.flow.next();
	},
	loadSpoon:function(pos){
		var spoon = new Button(this, 11, TAG_SPOON, "#spoon3.png",this.callback);
		this.loadInLib(spoon, pos, cc.p(700, 400), 1);
	},
	loadAir:function(pos){
		var air = new Button(this, 11, TAG_AIR, "#air.png",this.callback);
		var airFunnel = new Button(air, 7, TAG_AIR_FUNNEL, "#air_funnel.png",this.callback, this);
		airFunnel.setPosition(263, 165);
		this.loadInLib(air, pos, cc.p(250, 250), 1);
	},
	loadAirSide:function(){
		var airSide = new AirSize(this);
	},
	loadLamp:function(pos){
		// 650,190
		var lamp = new Lamp(this);
		lamp.setPosition(pos);
		var ber = $.bezier(lamp.getPosition(), cc.p(800,190), 1);
		var seq = cc.sequence(ber, this.callNext);
		lamp.runAction(seq);
	},
	loadMatch:function(pos){
		var match = new Match(this);
		match.setPosition(pos);
		match.lightMatch();
		var ber = $.bezier(match.getPosition(), cc.p(750,390), 0.5);
		var call = cc.callFunc(function(){
			this.doMatch();
		}, this);
		var seq = cc.sequence(ber, call);
		match.runAction(seq);
	},
	doMatch:function(){
		gg.flow.next();
	},
	loadMatchBox:function(pos){
		var match = new Match(this);
		match.setPosition(pos);
		var ber = $.bezier(match.getPosition(), cc.p(750,390), 1);
		var seq = cc.sequence(ber, this.callNext);
		match.runAction(seq);
	},
	loadShelf:function(pos){
		this.lampNode = new cc.Node();
		this.lampNode.setCascadeOpacityEnabled(true);
		this.addChild(this.lampNode,8,TAG_LAMP_NODE);

		var shelf = new cc.Sprite("#shelf.png");
		this.lampNode.addChild(shelf,3);
		this.loadInLib(shelf, pos, cc.p(650,200), 1);
	},
	loadNet:function(pos){
		var net = new cc.Sprite("#asbestosNet.png");
		this.lampNode.addChild(net,4);
		this.loadInLib(net, pos, cc.p(645,255), 1);
	},
	loadDrop:function(pos){
		var tag = null;
		var action = gg.flow.flow.tag[1];
		if(action == TAG_LIB_BACL2){
			tag = TAG_BACL;
		} else if(action == TAG_LIB_NAOH){
			tag = TAG_NAOH;
		} else if(action == TAG_LIB_NA2CO3){
			tag = TAG_NA2CO3;
		} else if(action == TAG_LIB_HCl){
			tag = TAG_HCL;
		} else if(action == TAG_LIB_ALCOHOL){
			tag = TAG_ALCOHOL;
		}
		
		var drop = new DropBottle(this, tag);
		this.loadInLib(drop, pos, cc.p(480,250), 1);
	},
	loadPot:function(pos){
		var pot = new Pot(this);
		pot.setPosition(pos);
		this.loadInLib(pot, pos, cc.p(645,275), 1);
	},
	loadPH:function(pos){
		var ph = new PhCard(this);
		ph.setPosition(pos);
		this.loadInLib(ph, pos, cc.p(500,400), 1);
	},
	loadWide:function(pos){
		var wide = new WideBottle(this);
		this.loadInLib(wide, pos, cc.p(600, 200), 1);
	},
	loadInLib:function(obj, pos, tarPos,delay){
		obj.setPosition(pos);
		if(delay == null){
			delay = 1;
		}
		var ber = $.bezier(pos, tarPos, delay);
		var seq = cc.sequence(ber, this.callNext);
		obj.runAction(seq);
	}
});